//
//  UserDataController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/30.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserDataController.h"
#import "UIView+Frame.h"
#import "PNChart.h"
#import "UserDataModel.h"
#import "UserAccountCell.h"
#import "UserAvgListCell.h"
#import "UserBestListCell.h"
#import "SportViewController.h"


#define ksectionHeadHeight kAutoHei(30)
#define ksectionFootHeight kAutoHei(160)

@interface UserDataController ()

@property (nonatomic, strong) UserDataModel *userDataModel;
@end

@implementation UserDataController {
    BOOL _isChinese;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _isChinese = [NSBundle currentLanguageIsChinese];
    [self setTableviewStyle];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"UserBestListCell" bundle:nil] forCellReuseIdentifier:@"dataControllerCellTwo"];
    [self addRefreshheader];
    [self.tableView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateUserData) name:SportViewControllerUpdateSportDataNotification object:nil];
}
- (void)UpdateUserData {
    [self loadUserData];
}
- (void)addRefreshheader {
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithShowTime:NO RefreshingBlock:^{
        [weakSelf loadUserData];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 加载网络数据
- (void)loadUserData {
    
    YFUserApi *getUserDataApi = [YFUserApi requestGetUserDataWithReqType:kUserGetYearHistoryDataApi];
    __weak typeof(self) weakSelf = self;
    [[YFNetworkingService sharedInstance] sendRequest:getUserDataApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            UserDataModel *personalDataModel = [[UserDataModel alloc] init];
            [personalDataModel parseUserDataDictionary:[result responseInfo]];
            weakSelf.userDataModel = personalDataModel;
            NSLog(@"%@",getUserDataApi.responseJSONObject);
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
}

#pragma mark 设置tableView的样式
- (void)setTableviewStyle {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // 设置背景色
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    // 去掉分割
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 1) {
        return self.userDataModel.bestList.count;
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"dataControllerCellOne" forIndexPath:indexPath];
        UserAccountCell *oneCell = (UserAccountCell *)cell;
        oneCell.dataModel = self.userDataModel;
    }else if (indexPath.section == 2){
        cell = [tableView dequeueReusableCellWithIdentifier:@"dataControllerCellThree" forIndexPath:indexPath];
        UserAvgListCell *avgCell = (UserAvgListCell *)cell;
        avgCell.dataModel = self.userDataModel;
    }else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"dataControllerCellTwo" forIndexPath:indexPath];
        UserBestListCell *bestListCell = (UserBestListCell *)cell;
        bestListCell.bestListModel = self.userDataModel.bestList[indexPath.row];
        
        if (indexPath.row == self.userDataModel.bestList.count - 1) {
            [bestListCell isShowBottomLine:NO];
        }else {
            [bestListCell isShowBottomLine:YES];
        }
        
        if (indexPath.row == 0) {
            [bestListCell isShowTopLine:NO];
        }else {
            [bestListCell isShowTopLine:YES];
        }
        if (indexPath.row == 0 && indexPath.row == self.userDataModel.bestList.count - 1) {
            [bestListCell isShowGradientLayer:YES];
            
        }else {
            [bestListCell isShowGradientLayer:NO];
        }
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return ksectionHeadHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1 && self.userDataModel.bestList.count == 0) {
        return ksectionFootHeight;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 1 && self.userDataModel.bestList.count == 0) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, ksectionFootHeight)];
    
        UIImage *image = [UIImage imageNamed:@"wushuju-0"];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kYFScreenWidth - image.size.width) * 0.5, 10, image.size.width, image.size.height)];
        imageView.image = image;
        [footerView addSubview:imageView];
        footerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), kYFScreenWidth, 30)];
        label.textAlignment = NSTextAlignmentCenter;
        
        label.text = _isChinese ? @"您还没有运动数据哦" :NSLocalizedString(@"No Motion Data", nil);
        label.textColor = [UIColor colorWithHex:0x787878];
        label.font = [UIFont systemFontOfSize:15];
        
        [footerView addSubview:label];
        
        // 创建分割线
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, 0.5)];
        topView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.15];
        
        UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, ksectionFootHeight - 0.5, kYFScreenWidth, 0.5)];
        bottomLineView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.15];
        
        [footerView addSubview:topView];
        [footerView addSubview:bottomLineView];
        
        return footerView;
    }else {
        return [[UIView alloc] init];
    }
 
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, ksectionHeadHeight)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kYFScreenWidth - 20, headView.bounds.size.height)];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor colorWithHex:0x151515 alpha:0.5];
    [headView addSubview:label];
    headView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    
    YFLanguage currentLanguage = [NSBundle currentLangeuage];
    
    if (section == 0) {
        label.text =  _isChinese ? @"累计" : @"Total";
    }else if (section == 1){
        label.text = _isChinese ? @"最佳" :(currentLanguage == kLanguageFrennch ? @"Meilleur":@"Best");
    }else if (section == 2){
        label.text = _isChinese ? @"平均":(currentLanguage == kLanguageFrennch ? @"Moyenne":@"Average");
    }
    return headView;
}


@end
